﻿Imports System.Data.OleDb
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Xml
Imports System.Text
Imports System.IO.Stream
Imports System.Text.Encoding
Public Class rssfeed
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Get connection string from the web.config file  
        Dim connString As String = ConfigurationManager.ConnectionStrings("Fishconn").ConnectionString

        ' Create SqlConnection object  
        Dim dataConn As OleDbConnection = New OleDbConnection()
        dataConn.ConnectionString = connString
        Dim dt As DataTable = New DataTable()
        Dim dr As OleDbDataReader

        Response.Clear()
        Response.ContentType = "application/rss+xml"
        Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
        objX.WriteStartDocument()
        objX.WriteStartElement("rss")
        objX.WriteAttributeString("version", "2.0")
        objX.WriteStartElement("channel")
        ' SQL query to retrieve data from database  
        Dim sqlQuery As String = "SELECT [FirstName] + ' ' + [LastName] AS Name,  [Gender] as sex , [ProfilePicturesmall], [Country] as Country, 'details.aspx?ID=' + cstr(tblProfile.[UserID]) as link FROM tblProfile"

        ' Create SqlCommand object  
        Dim cmd As OleDbCommand = New OleDbCommand()
        cmd.Connection = dataConn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = sqlQuery
        dataConn.Open()
        dr = cmd.ExecuteReader()
        objX.WriteElementString("title", "Fishing in Grenada")
        objX.WriteElementString("link", "default.aspx")
        objX.WriteElementString("description", "Blogs by Hainsley")
        objX.WriteElementString("language", "en-us")
        objX.WriteElementString("ttl", "5")
        objX.WriteElementString("lastBuildDate", String.Format("{0:R}", DateTime.Now))

        While (dr.Read())
            objX.WriteStartElement("item")
            objX.WriteElementString("title", "By " + dr("Name").ToString.ToString())
            objX.WriteElementString("author", dr("Name").ToString.ToString())
            objX.WriteElementString("Name", dr("Name").ToString.ToString())
            objX.WriteElementString("sex", dr("sex").ToString())
            objX.WriteElementString("Country", dr("country").ToString())
            objX.WriteElementString("link", dr("link").ToString())
            objX.WriteEndElement()

        End While

        objX.WriteEndElement()
        objX.WriteEndElement()
        objX.WriteEndDocument()
        objX.Flush()
        objX.Close()
        Response.End()
    End Sub

End Class